package com.atfzr.interrupt;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;

/**
 * @author 望夜
 * @date 2023/8/20---22:05
 * @SuppressWarnings({"all"})  中断机制：  线程自己的命运由自己决定
 */
public class InterruptDemo {
    static volatile boolean isStop = false;
    static AtomicBoolean atomicBoolean = new AtomicBoolean(false);

    public static void main(String[] args) {
        Thread t1 = new Thread(() -> {
            while (true) {
                if (Thread.currentThread().isInterrupted()) {
                    System.out.println(Thread.currentThread().getName() + "\t isInterrupted()被修改为true,程序停止");
                    break;
                }
                System.out.println("t1  -----hello interrupt api");
            }
        }, "t1");
        t1.start();

        System.out.println("------t1的默认中断标志位: " + t1.isInterrupted());

        try {
            //暂停毫秒
            TimeUnit.MILLISECONDS .sleep(20);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        new Thread(()->{
            t1.interrupt(); //发出中断协商申请
        },"t2").start();
    }


}
